package com.sz.admin.serviceorder.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.sz.admin.serviceorder.pojo.dto.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.sz.admin.serviceorder.service.ServiceOrderService;
import com.sz.admin.serviceorder.pojo.po.ServiceOrder;
import com.sz.admin.serviceorder.mapper.ServiceOrderMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.query.QueryChain;
import com.sz.core.common.enums.CommonResponseEnum;
import com.sz.core.util.PageUtils;
import com.sz.core.util.BeanCopyUtils;
import com.sz.core.util.Utils;
import com.sz.core.common.entity.PageResult;
import com.sz.core.common.entity.SelectIdsDTO;
import java.io.Serializable;
import java.util.List;

import org.springframework.web.multipart.MultipartFile;
import com.sz.excel.core.ExcelResult;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import com.sz.excel.utils.ExcelUtils;
import lombok.SneakyThrows;
import com.sz.admin.serviceorder.pojo.vo.ServiceOrderVO;

/**
 * <p>
 * 服务订单表 服务实现类
 * </p>
 *
 * @author sz-admin
 * @since 2024-10-16
 */
@Service
@RequiredArgsConstructor
public class ServiceOrderServiceImpl extends ServiceImpl<ServiceOrderMapper, ServiceOrder> implements ServiceOrderService {
    @Override
    public void create(ServiceOrderCreateDTO dto){
        ServiceOrder serviceOrder = BeanCopyUtils.copy(dto, ServiceOrder.class);
        save(serviceOrder);
    }

    @Override
    public void update(ServiceOrderUpdateDTO dto){
        ServiceOrder serviceOrder = BeanCopyUtils.copy(dto, ServiceOrder.class);
        QueryWrapper wrapper;
        // id有效性校验
        wrapper = QueryWrapper.create()
            .eq(ServiceOrder::getId, dto.getId());
        CommonResponseEnum.INVALID_ID.assertTrue(count(wrapper) <= 0);

        saveOrUpdate(serviceOrder);
    }

    @Override
    public PageResult<ServiceOrderVO> page(ServiceOrderListDTO dto){
        Page<ServiceOrderVO> page = pageAs(PageUtils.getPage(dto), buildQueryWrapper(dto), ServiceOrderVO.class);
        return PageUtils.getPageResult(page);
    }

    @Override
    public List<ServiceOrderVO> list(ServiceOrderListDTO dto){
        return listAs(buildQueryWrapper(dto), ServiceOrderVO.class);
    }

    @Override
    public void remove(SelectIdsDTO dto){
        CommonResponseEnum.INVALID_ID.assertTrue(dto.getIds().isEmpty());
        removeByIds(dto.getIds());
    }

    @Override
    public ServiceOrderVO detail(Object id){
        ServiceOrder serviceOrder = getById((Serializable) id);
        CommonResponseEnum.INVALID_ID.assertNull(serviceOrder);
        return BeanCopyUtils.copy(serviceOrder, ServiceOrderVO.class);
    }

    @SneakyThrows
    @Override
    public void importExcel(MultipartFile file) {
        ExcelResult<ServiceOrderImportDTO> excelResult = ExcelUtils.importExcel(file.getInputStream(), ServiceOrderImportDTO.class, true);
        List<ServiceOrderImportDTO> list = excelResult.getList();
        List<String> errorList = excelResult.getErrorList();
        String analysis = excelResult.getAnalysis();
        System.out.println(" analysis : " + analysis);
    }

    @SneakyThrows
    @Override
    public void exportExcel(ServiceOrderListDTO dto, HttpServletResponse response) {
        List<ServiceOrderVO> list = list(dto);
        ServletOutputStream os = response.getOutputStream();
        ExcelUtils.exportExcel(list, "服务订单表", ServiceOrderVO.class, os);
    }

    /**
     * 查询当前用户下的订单
     * @param dto
     * @return
     */
    @Override
    public PageResult<ServiceOrderVO> pageByUser(ServiceOrderwxDTO dto){
        // 获取当前登录的用户id
        String loginId = StpUtil.getLoginIdAsString();
        dto.setCreateId(loginId);
        // 查询当前用户关联的订单
        QueryWrapper wrapper = QueryWrapper.create().from(ServiceOrder.class);
        wrapper.eq(ServiceOrder::getCreateId,dto.getCreateId());

        Page<ServiceOrderVO> page = pageAs(PageUtils.getPage(dto), wrapper, ServiceOrderVO.class);
        return PageUtils.getPageResult(page);
    }

    private static QueryWrapper buildQueryWrapper(ServiceOrderListDTO dto) {
        QueryWrapper wrapper = QueryWrapper.create().from(ServiceOrder.class);
        if (Utils.isNotNull(dto.getFigure())) {
            wrapper.eq(ServiceOrder::getFigure, dto.getFigure());
        }
        if (Utils.isNotNull(dto.getStandards())) {
            wrapper.eq(ServiceOrder::getStandards, dto.getStandards());
        }
        if (Utils.isNotNull(dto.getSulfurNum())) {
            wrapper.eq(ServiceOrder::getSulfurNum, dto.getSulfurNum());
        }
        if (Utils.isNotNull(dto.getInitDitchDepth())) {
            wrapper.eq(ServiceOrder::getInitDitchDepth, dto.getInitDitchDepth());
        }
        if (Utils.isNotNull(dto.getUserPhone())) {
            wrapper.eq(ServiceOrder::getUserPhone, dto.getUserPhone());
        }
        if (Utils.isNotNull(dto.getAddress())) {
            wrapper.eq(ServiceOrder::getAddress, dto.getAddress());
        }
        if (Utils.isNotNull(dto.getDescribe())) {
            wrapper.eq(ServiceOrder::getDescribe, dto.getDescribe());
        }
        if (Utils.isNotNull(dto.getImgUrl())) {
            wrapper.eq(ServiceOrder::getImgUrl, dto.getImgUrl());
        }
        if (Utils.isNotNull(dto.getSalesDateStart()) && Utils.isNotNull(dto.getSalesDateEnd())) {
            wrapper.between(ServiceOrder::getSalesDate, dto.getSalesDateStart(), dto.getSalesDateEnd());
        }
        if (Utils.isNotNull(dto.getFaultType())) {
            wrapper.eq(ServiceOrder::getFaultType, dto.getFaultType());
        }
        if (Utils.isNotNull(dto.getLogisticsRemarks())) {
            wrapper.eq(ServiceOrder::getLogisticsRemarks, dto.getLogisticsRemarks());
        }
        if (Utils.isNotNull(dto.getEngineerRemarks())) {
            wrapper.eq(ServiceOrder::getEngineerRemarks, dto.getEngineerRemarks());
        }
        if (Utils.isNotNull(dto.getPaymentMode())) {
            wrapper.eq(ServiceOrder::getPaymentMode, dto.getPaymentMode());
        }
        return wrapper;
    }
}